while abs(wage_distance)>1e-2 && counter<=1000    
    counter=counter+1;
    
    omega = wage_US / wage.guess_OECD;

    % ASSIGN EXPORTS AND IMPORTS BASED ON GUESSED RELATIVE WAGE
    relative_quality=qualities_vec_US ./ qualities_vec_OECD;
    ordered_quality=sort(relative_quality,'descend');

    exports_US = relative_quality>import_tariff_OECD*omega;
    exports_OECD = relative_quality*import_tariff_US<omega;
    
    x_guess = sum(exports_US)/n_varieties;
    m_guess = sum(exports_OECD)/n_varieties;

    produced_US = ~exports_OECD;
    produced_OECD = ~exports_US;
    
    nontraded_US = ~exports_US & ~exports_OECD;
    nontraded_OECD = nontraded_US;
    
    % TRACK COMPETITORS FOR AND CALCULATE PRICES AND MARKUPS
    next_best_qual_vec_US = max(domestic_competitor_qual_vec_US/wage_US,qualities_vec_OECD/(import_tariff_US*wage.guess_OECD));
    is_domestic_competitor_exports_US = domestic_competitor_qual_vec_US/(import_tariff_OECD*wage_US)>qualities_vec_OECD/(wage.guess_OECD);

    next_best_qual_vec_OECD = max(domestic_competitor_qual_vec_OECD/wage.guess_OECD,qualities_vec_US/(import_tariff_OECD*wage_US));
    is_domestic_competitor_exports_OECD = domestic_competitor_qual_vec_OECD/(import_tariff_US*wage.guess_OECD)>qualities_vec_US/(wage_US);

    prices_US = qualities_vec_US./ next_best_qual_vec_US;

    prices_US(exports_OECD & is_domestic_competitor_exports_OECD) = (qualities_vec_OECD(exports_OECD & is_domestic_competitor_exports_OECD) ./ ...
            domestic_competitor_qual_vec_OECD(exports_OECD & is_domestic_competitor_exports_OECD)) * import_tariff_US * wage.guess_OECD;

    prices_US(exports_OECD & ~is_domestic_competitor_exports_OECD) = (qualities_vec_OECD(exports_OECD & ~is_domestic_competitor_exports_OECD) ./ ...
            qualities_vec_US(exports_OECD & ~is_domestic_competitor_exports_OECD)) * wage_US;
    
    markups_US = prices_US / wage_US;
    markups_US(exports_OECD) = prices_US(exports_OECD) / (import_tariff_US * wage.guess_OECD);
    
    prices_OECD = qualities_vec_OECD./ next_best_qual_vec_OECD;

    prices_OECD(exports_US & is_domestic_competitor_exports_US) = (qualities_vec_US(exports_US & is_domestic_competitor_exports_US) ./ ...
            domestic_competitor_qual_vec_US(exports_US & is_domestic_competitor_exports_US)) * wage_US * import_tariff_OECD;

    prices_OECD(exports_US & ~is_domestic_competitor_exports_US) = (qualities_vec_US(exports_US & ~is_domestic_competitor_exports_US) ./ ...
            qualities_vec_OECD(exports_US & ~is_domestic_competitor_exports_US)) * wage.guess_OECD;

    markups_OECD = prices_OECD / wage.guess_OECD;
    markups_OECD(exports_US) = prices_OECD(exports_US) / (import_tariff_OECD * wage_US);
    
    % CALCULATE RELATIVE WAGE IMPLIED BY BALANCED TRADE AND COMPARE TO
    % GUESS. ITERATE UNTIL THEY ARE THE SAME.
    wage.balanced_trade_OECD = abs(((mu_bar.step7_US*wage_US*L_US*m_guess*import_tariff_OECD)/(x_guess*import_tariff_US)-...
        (import_tariff_OECD-1) * export_share.step6_US * mu_bar.step7_US * wage_US * L_US)...
        /(mu_bar.step7_OECD * L_OECD - (import_tariff_US-1) * (m_guess * import_tariff_OECD) / (x_guess * import_tariff_US) * export_share.step6_OECD * mu_bar.step7_OECD * L_OECD));
    wage_distance = wage.balanced_trade_OECD - wage.guess_OECD;
   
  
        wage.guess_OECD = wage.guess_OECD + (0.05*wage_distance);

    
   if counter>950
     wage_bounce_list = [wage_bounce_list; wage.guess_OECD];
   end 

end
 % IF THE ITERATION DOESN'T CONVERGE TO THE TOLERANCE LEVEL, TAKE THE WAGE
 % IMPLIED BY THE LAST 50 WAGE GUESSES THAT BOUNCED AROUND THE EQUILIBRIUM
if counter==1000
    wage.balanced_trade_OECD=mean(wage_bounce_list);
end